package com.big.common.mybatis.util;

import cn.hutool.core.lang.Assert;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.big.common.core.result.PageQuery;

/**
 * <p>
 * 分页查询工具类
 * </p>
 *
 * @author 山归岿
 * @create 2025-06-21-18:14
 */
public record PageQueryWrapper<T, DTO>(Page<T> page, QueryWrapper<T> queryWrapper, DTO dto) {
    public PageQueryWrapper {
        Assert.notNull(page, "Page 不能为 null");
        Assert.notNull(queryWrapper, "QueryWrapper 不能为 null");
        Assert.notNull(dto, "T 不能为 null");
    }

    public static <T, DTO> PageQueryWrapper<T, DTO> of(PageQuery<DTO> pageQuery) {
        // 创建分页对象
        Page<T> page = MybatisPageUtils.createPage(pageQuery);
        // 处理排序和高级查询
        QueryWrapper<T> queryWrapper = MybatisPageUtils.handleOrderByAndQuery(pageQuery, "create_time");
        return new PageQueryWrapper<>(page, queryWrapper, pageQuery.getQuery());
    }
}
